﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ArasPLMExtend
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            txtArasPLMUrl.Text = ArasConnection.PublicState.PLMUrl;
            if(!String.IsNullOrEmpty(txtArasPLMUrl.Text.Trim()))
            {
                ArasConnection.PublicState.PLMUrl = txtArasPLMUrl.Text.Trim();
                cbxArasDatabase.DataSource = ArasConnection.AConnection.getArasPLMDatabases();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void txtArasPLMUrl_Leave(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtArasPLMUrl.Text.Trim()))
            {
                ArasConnection.PublicState.PLMUrl = txtArasPLMUrl.Text.Trim();
                cbxArasDatabase.DataSource = ArasConnection.AConnection.getArasPLMDatabases();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            ArasConnection.PublicState.PLMUrl = txtArasPLMUrl.Text.Trim();
            ArasConnection.PublicState.PLMDatabase = cbxArasDatabase.Text.Trim();
            ArasConnection.PublicState.PLMUserName = txtLoginUser.Text.Trim();
            ArasConnection.PublicState.PLMPassword = txtLoginPass.Text.Trim();
            if(ArasConnection.AConnection.checkArasLogin())
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void txtLoginUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==(char)Keys.Enter)
            {
                txtLoginPass.Focus();
            }

        }

        private void txtLoginPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==(char)Keys.Enter)
            {
                btnLogin_Click(sender, e);
            }
        }
    }
}
